#!/bin/bash
# use this script to record each disk, ethernet card speed per second
# and average speed for total

if [[ $# != 1 ]] ; then
    echo "Usage : $0 [acctime]"
    exit -1
fi

eth0=0
eth1=0
eth2=0
eth3=0
bond0=0

disk0=0
disk1=0
disk2=0
disk3=0
disk4=0
disk5=0
disk6=0
disktotal=0

#initiallization

for ((i=0; i< 5; i++)); do
    eth[$i]=0
done

for ((i=0; i< 7; i++)); do
    disk[$i]=0
done


file=/tmp/$RANDOM

#rm ././*.dat -f

cout=0

while : ; do
    let "cout=$cout+1"

    sar -b -d -n DEV 1 1 > $file
    disk=`cat $file | head -n 4 | tail -1 | awk '{print $6}'`
    disk=`echo "scale=3; $disk/2000.0" | bc` 
    disktotal=`echo "scale=3; ($disk + $disktotal)" | bc`
    avg=`echo "scale=3; $disktotal / $cout" | bc`
    echo "$cout $disk" >> ./disktotalspeed.dat
    echo "$cout $avg" >> ./disktotalavg.dat

    for ((i=7, j=0; i < 13; i++, j++)); do
        disk=`cat $file | head -n $i | tail -1 | awk '{print $5}'`
        disk=`echo "scale=3; $disk/2000.0" | bc`
        disk[$j]=`echo "scale=3; ($disk + ${disk[$j]})" | bc`
        avg=`echo "scale=3; ${disk[$j]} / $cout" | bc`
        echo "$cout $disk" >> ./disk${j}speed.dat
        echo "$cout $avg" >> ./disk${j}avg.dat
    done

    for ((i=16, j=0; i < 19; i++, j++)); do
        net=`cat $file | head -n $i | tail -1 | awk '{print $7}'`
        net=`echo "scale=3; $net/1000.0" | bc` 
        eth[$j]=`echo "scale=3; ($net+ ${eth[$j]})" | bc`
        avg=`echo "scale=3; ${eth[$j]} / $cout" | bc`
        echo $cout "$net" >> ./net${j}speed.dat
        echo $cout "$avg" >> ./net${j}avg.dat
    done

# time control
    echo $cout 
    if [[ $cout == $1 ]]; then
        break;
    fi
done
